package com.shg.service.impl;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;


import org.hibernate.Query;
import org.springframework.stereotype.Service;

import com.shg.bean.Author;
import com.shg.bean.PageView;
import com.shg.bean.QueryResult;
import com.shg.bean.Users;
import com.shg.service.AuthorService;

@Service
public class AuthorServiceBean extends BasiceDaoImpl<Author> implements AuthorService {

	/**
	 * 查询最红作者显示在首页
	 * @param maxresult 查询的最大显示记录数
	 * @return
	 */
	public QueryResult<Author> getBestAuthor(PageView<Author> pageView){
		LinkedHashMap<String, String> orderby=new LinkedHashMap<String, String>();
		orderby.put("marker", "desc");
		QueryResult<Author> queryResult= getPagination(Author.class, orderby);
		List<Author> list=queryResult.getResultList();
		for(Author author:list){
			Users user= (Users) sessionFactory.getCurrentSession().get(Users.class, author.getId());
			author. setUsers(user);
		}
		return queryResult;
	}
	/**
	 * 查询前几位作者
	 */
	@SuppressWarnings("unchecked")
	public Map<String, Object> getBestAuthor() {
		Map<String, Object> map=new HashMap<String, Object>();
		String sql="from Author where marker=1";
		List<Author> list=sessionFactory.getCurrentSession().createQuery(sql).list();
		for(Author author:list){
			Users user= (Users) sessionFactory.getCurrentSession().get(Users.class, author.getId());
			author. setUsers(user);
		}
		map.put("authorMap", list);
		return map;
	}
	/**
	 * 查询最新的几位作者
	 * @param maxresult 查询的最大记录数
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Author> getNewAuthor(int maxresult){
		String sql="from "+Author.class.getSimpleName()+" order by times desc";
		Query query=sessionFactory.getCurrentSession().createQuery(sql).setMaxResults(maxresult);
		List<Author> list=query.list();
		for(Author author:list){
			author. getUsers().setUserName(author.getUsers().getUserName());
		}
		return list;
	}
	/**
	 * 查询知名度最该的几位作者
	 */
	public List<Author> getLove(Integer maxResult){
		//查询书籍 如果shopId为null是统计这个网站的销售的书籍  否则为统计整个店铺的销售书籍
		String sql="from "+Author.class.getSimpleName()+" order by love asc";
		List<Author> authors = sessionFactory.getCurrentSession().createQuery(sql).setMaxResults(maxResult).list();
		return authors;
	}


}
